package com.liu.qqRobot.myself.handler;

import cn.hutool.core.codec.Base64;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@Slf4j
@Component
public class BotMenuHandler {

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        String text = data.getText();
        String param = text.replace("菜单 ", "");
        String templateName;
        String generateFilePath;
        switch (param) {
            case "wf" -> {
                templateName = "wfHelp.html";
                generateFilePath = "src/main/resources/picture/wfHelp.png";
            }
            case "poke" -> {
                templateName = "pokeHelp.html";
                generateFilePath = "src/main/resources/picture/pokeHelp.png";
            }
            default -> {
                return;
            }
        }
        try {
            HtmlToImageUtils.htmlToImage(templateName, new HashMap<>(), generateFilePath, 3000);
            File file = new File(generateFilePath);
            String encode = Base64.encode(file);
            String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
            QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.AT_BOT);
        } catch (IOException | ParserConfigurationException | SAXException | TemplateException e) {
            log.error("获取帮助展示模板失败:{}", e.getMessage(), e);
//            QQClientUtils.sendGroupMsg("[CQ:at,qq=" + userId + "]\n获取捕捉宝可梦展示模板失败，请联系制作人", userId, groupId, isGroup);
        }
    }

}
